package com.sali.DP;

import java.util.HashMap;

/**
 * @author sali
 * @date 2025/5/27 20:44
 */
public class JZ_48 {

    public static int lengthOfLongestSubstring (String s) {
        if ( s == null || s.length() == 0 ) {
            return 0;
        }
        if ( s.length() == 1 ) {
            return 1;
        }
        int left = 0;
        int maxLen = 0;
        HashMap<Character, Integer> myDic = new HashMap<>();

        char[] myArr = s.toCharArray();

        for ( int right = 0; right < myArr.length; right++ ) {
            if ( myDic.containsKey(myArr[right]) && myDic.get(myArr[right]) >= left ) {
                left = myDic.get(myArr[right]) + 1;
            }
            myDic.put(myArr[right], right);
            maxLen = Math.max(maxLen, right - left + 1);
        }

        return maxLen;
    }

}
